/**
 * 英雄坦克类
 * 英雄坐标
 * 移动方向
 * 颜色
 * @param {Object} x
 * @param {Object} y
 * @param {Object} direct
 * @param {Object} color
 */
function Hero(x,y,direct,color) {
	this.x = x;
	this.y = y;
	this.direct = direct;

	this.life  = 10;
	
	this.color = color;
}

Hero.prototype = new Tank();
Hero.prototype.moveUp = function() {
	this.y -= this.speed;
	if ( this.y < 0 ) this.y = 0;
	// 砖块判断
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.y = bricks[i].y+bricks[i].h;
		}
	}
	// 钢板判断
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.y = steels[i].y+steels[i].h;
		}
	}
	this.direct = 0;
};

Hero.prototype.moveRight = function() {
	this.x += this.speed;
	if ( this.x > 500 - 30 ) this.x = 500 - 30;
	// 砖块判断
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.x = bricks[i].x - this.w;
		}
	}
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.x = steels[i].x - this.w;
		}
	}
	this.direct = 1;
};

Hero.prototype.moveDown = function() {
	this.y += this.speed;
	if ( this.y > 500 - 30 ) this.y = 500 - 30;
	// 砖块判断
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.y = bricks[i].y - this.h;
		}
	}
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.y = steels[i].y - this.h;
		}
	}
	this.direct = 2;
};

Hero.prototype.moveLeft = function() {
	this.x -= this.speed;
	if ( this.x < 0 ) this.x = 0;
	// 砖块判断
	for (var i = 0 ; i < bricks.length; i++) {
		if ( checkPZ( this , bricks[i] ) ){
			this.x = bricks[i].x + bricks[i].w;
		}
	}
	for (var i = 0 ; i < steels.length; i++) {
		if ( checkPZ( this , steels[i] ) ){
			this.x = steels[i].x + steels[i].w;
		}
	}
	this.direct = 3;
};
Hero.prototype.reset = function () {
	this.x = 220;
	this.y = 470;
}
Hero.prototype.fire = function() {
	if ( !this.isShot ){
		this.isShot = !this.isShot;
		if ( this.direct === 0 )
			this.bullet = new Bullet( this.x + 15 ,this.y ,this.direct );
		if ( this.direct === 1 )
			this.bullet = new Bullet( this.x + 30 ,this.y + 15 ,this.direct );
		if ( this.direct === 2)
			this.bullet = new Bullet( this.x + 15 ,this.y + 30 ,this.direct );
		if ( this.direct === 3)
			this.bullet = new Bullet( this.x      ,this.y + 15 ,this.direct );
		this.shotInterval = setInterval(bulletRun, 10);
		var _this = this;
		function bulletRun () {
			
			switch ( _this.bullet.direct ){
				case 0: 
					_this.bullet.y -= _this.bullet.speed;
				break;
				case 1: 
					_this.bullet.x += _this.bullet.speed;
				break;
				case 2: 
					_this.bullet.y += _this.bullet.speed;
				break;
				case 3: 
					_this.bullet.x -= _this.bullet.speed;
				break;
			}
			// 我方坦克打中敌军
			for (var i = 0; i < showEnemies.length; i++) {
				if( pz( showEnemies[i] , _this.bullet ) ){
					// 子弹重置
					clearInterval(_this.shotInterval)
					_this.bullet = null;
					_this.isShot = !_this.isShot;
					// 判断生命值是否为0
					console.log( showEnemies[i].life )
					showEnemies[i].life--;
					if( showEnemies[i].life <= 0 ){
						// 清除敌军移动定时器
						clearInterval(showEnemies[i].timer);
						// 清除敌军子弹定时器
						clearInterval(showEnemies[i].shotInterval);
						// 清楚敌军子弹
						showEnemies[i].bullet = null;
						// 清除敌军
						showEnemies.splice(i, 1);
					}
					return;
				}
			}
			if ( _this.bullet.x < 0 || _this.bullet.x > 500 ||
					 _this.bullet.y < 0 || _this.bullet.y > 500){
				_this.bullet = null;
				clearInterval(_this.shotInterval)
				_this.isShot = !_this.isShot;
			}
			// 打中墙壁 
			// 销毁子弹
			// 销毁墙壁
			if( _this.bullet )
			{
				for (var i = 0 ; i < bricks.length; i++) {
					if ( checkPZ( _this.bullet , bricks[i] ) ){
						bricks.splice(i , 1);
						_this.bullet = null;
						clearInterval(_this.shotInterval);
						_this.isShot = !_this.isShot;
						break;
					}
				}
				// 打中钢板
				// 只销毁子弹
				for (var i = 0 ; i < steels.length; i++) {
					if ( checkPZ( _this.bullet , steels[i] ) ){
						// steels.splice(i , 1);
						clearInterval(_this.shotInterval);
						_this.bullet = null;
						_this.isShot = !_this.isShot;
						break;
					}
				}
			}
		};
	}
};